<?php
class Administrator_BlogController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}
		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('12', $strarray)) {
        		exit('no priviage');
        	}
		}
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		$page=$this->_request->getParam('page');
		$item=new CItem();		
		$itemCounts=$item->getAllItemCounts('blog');
    	Zend_Loader::loadClass("CFilter");
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 5 );
		$itemArray=$item->getAllItems($pageArray['from'],5,'blog');
		Zend_Loader::loadClass("CPage");
    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-blog-index?"); //生成分页字符串
    	$this->view->assign ( "pageString", $pageString );
    	$this->view->assign ( "pageArray", $pageArray );
		$this->view->assign('itemArray',$itemArray);
	}
	
	public function addAction()
	{
		if ($this->getRequest()->isPost()) {
			$this->getHelper('viewRenderer')->setNoRender();
			$thisArray=$this->_request->getParams();
			$filters = array(
	    		'*'   => 'StringTrim'
			);
			$validators = array();
			$input = new Zend_Filter_Input($filters, $validators, $thisArray);
			/*插入到条目表 start*/
			$item=new CItem();
			$itemData=array(
							'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
							'flid'=>0,
							'subject'=>$input->subject,
							'item_type'=>'blog',
							'add_time'=>time(),
							'update_time'=>time(),
							'is_banned'=>'0',
					  );
			$itemid=$item->createItem($itemData);
	    	/*插入到条目表 end*/
			$content=htmlspecialchars_decode($input->FCKeditor1);
			$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
			
	    	/*插入到公告表 start*/
			$blog = new CBlog();
			$data=array(
							'itemid'=>$itemid,							
							'content'=>$content,
						 );	
			$blog->add($data);
			/*插入到公告表 end*/			
			$this->view->showMsg("发表成功!",'administrator-blog-index');
		} else {
			require_once (MY_ROOT."/fckeditor/fckeditor.php");		
			$oFCKeditor = new FCKeditor('FCKeditor1') ;
			$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
			$oFCKeditor->Value		= '&nbsp';
			$oFCKeditor->Height=400;
			$oFCKeditor->Width="98%";
			$this->view->assign('oFCKeditor',$oFCKeditor);		
		}
	}
	
	public function editAction()
	{
		if ($this->getRequest()->isPost()) {
			$this->getHelper('viewRenderer')->setNoRender();
			$paramArray=$this->_request->getParams();
			$itemid=$paramArray['itemid'];
			$filters = array(
	    		'*'   => 'StringTrim'
			);
			$validators = array(
			);
			$input = new Zend_Filter_Input($filters, $validators, $paramArray);	
			/*修改条目表 start*/
			$item=new CItem();
			$itemData=array(
							'flid'=>0,
							'subject'=>$input->subject,
							'update_time'=>time(),
					  );
			$item->editItem($itemid,$itemData);
			/*修改条目表 end*/
			$content=htmlspecialchars_decode($input->FCKeditor1);
			$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
	    	/*修改公告表 start*/
			$blog = new CBlog();
			$data=array(
						 'content'=>$content,
					    );	
			$blog->edit($itemid,$data);
			/*修改公告表 end*/
			$this->view->showMsg("修改成功!",'administrator-blog-index');
			
		} else {
			$itemid=$this->_request->getParam('itemid');
	    	$item=new CItem();
	    	$itemArray=$item->getDetail($itemid,'blog',false);
	    	$this->view->assign('itemArray',$itemArray);
			require_once (MY_ROOT."/fckeditor/fckeditor.php");
			$oFCKeditor = new FCKeditor('FCKeditor1') ;
			$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
			$oFCKeditor->Value		= $itemArray[0]['content'];
			$oFCKeditor->Height=400;
			$oFCKeditor->Width="98%";
			$this->view->assign('oFCKeditor',$oFCKeditor);	
		}
	}
	
	public function deleteAction()
	{
		$this->_helper->viewRenderer->setNoRender();
    	$itemid = $this->_request->getParam('itemid');
    	if ($itemid == '') {
    		$itemidArray = $this->_request->getParam('itemArray');
    	} else {    		
    		$itemidArray=array($itemid);    		
    	}
    	$blog = new CBlog();
    	$item=new CItem();    	
    	$blog->delete($itemidArray);    	
    	$item->delItems($itemidArray);
    	$this->view->showMsg('删除成功!');
	}
	
	public function commentAction()
	{
		$blogComment = new CBlogComments();
		$page = intval($this->getRequest()->getParam('page'));
		$blogId = intval($this->getRequest()->getParam('id'));
		$pageNum = 5;
		if ($page == 0) {
			$page = 1;
		}
		if ($blogId == 0) {
			return;
		}
		$pageCount = $blogComment->getCommentsCount($pageNum, $blogId);
		if ($page > $pageCount && $pageCount != 0) {
			$page = $pageCount;
		}
		$comment = $blogComment->getComments($page, $pageNum, $blogId);
		$pageString = CPage::divPages( $pageCount, $page, ROOT_PATH."/administrator-blog-comment?id=" . $blogId);
		$this->view->comment = $comment;
		$this->view->pageCount = $pageCount;
		$this->view->pageString = $pageString;
		
	}
	
	public function deletecommentAction()
	{
		$this->_helper->viewRenderer->setNoRender();
    	$itemid = $this->_request->getParam('itemid');
    	if ($itemid == '') {
    		$itemidArray = $this->_request->getParam('itemArray');
    	} else {    		
    		$itemidArray=array($itemid);    		
    	}
    	$blogComment = new CBlogComments();    	
    	$blogComment->delete($itemidArray);    	
    	$this->view->showMsg('删除成功!');
	}
}